﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ComplexNumbers
{
    class ComplexNumber
    {
        double real, imagine;

        double Real
        {
            get
            {
                return real;
            }
        }

        double Imagine
        {
            get
            {
                return imagine;
            }
        }

        public ComplexNumber(double real, double imagine)
        {
            this.real = real;
            this.imagine = imagine;
        }

        public static ComplexNumber operator +(ComplexNumber c1, ComplexNumber c2)
        {
            return new ComplexNumber(c1.Real + c2.Real, c1.Imagine + c2.Imagine);
        }

        public static ComplexNumber operator -(ComplexNumber c1, ComplexNumber c2)
        {
            return new ComplexNumber(c1.Real - c2.Real, c1.Imagine - c2.Imagine);
        }

        public static ComplexNumber operator /(ComplexNumber c1, ComplexNumber c2)
        {
            return new ComplexNumber((c1.Real * c2.Real + c1.Imagine * c2.Imagine) / 
                                     (c2.Real * c2.Real + c2.Imagine * c2.Imagine),
                                     (c1.Imagine * c2.Real - c1.Real * c2.Imagine) / 
                                     (c2.Real * c2.Real + c2.Imagine * c2.Imagine));
        }

        public static ComplexNumber operator *(ComplexNumber c1, ComplexNumber c2)
        {
            return new ComplexNumber(c1.Real * c2.Real - c1.Imagine * c2.Imagine, c1.Real * c2.Imagine + c1.Imagine * c2.Real);
        }

        public String ToString()
        {
            return "(" + this.Real.ToString() + ", " + this.Imagine.ToString() + ")";
        }

        public static explicit operator double(ComplexNumber C)
        {
            if (Math.Abs(C.Imagine) < 1e-9)
            {
                return C.Real;
            }
            else
            {
                return Double.NaN;
            }
        }

        public static implicit operator ComplexNumber(double A)
        {
            return new ComplexNumber(A, 0);
        }

        public double this[int index]
        {
            get
            {
                if (index == 0)
                {
                    return this.Real;
                }
                else if (index == 1)
                {
                    return this.Imagine;
                }
                else
                {
                    throw new ArgumentOutOfRangeException("Index out of range!");
                }
            }

        }

        static void Main(string[] args)
        {

        }
    }
}
